/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.ybq.parallaxviewpager.slice;

import com.github.ybq.parallaxviewpager.ImageDirectionalLayout;
import com.github.ybq.parallaxviewpager.Interpolator.DecelerateInterpolator;
import com.github.ybq.parallaxviewpager.LogUtil;
import com.github.ybq.parallaxviewpager.Mode;
import com.github.ybq.parallaxviewpager.ParallaxViewPager;
import com.github.ybq.parallaxviewpager.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ListDialog;

import java.util.ArrayList;

/**
 * The type Main ability slice.
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final int LEFT_OVERLAY = 0;
    private static final int RIGHT_OVERLAY = 1;
    private static final int NONE = 2;
    /**
     * The Pageview.
     */
    ArrayList<Component> pageview;
    private ParallaxViewPager parallaxViewPager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
        Button btnChangeMode = (Button) findComponentById(ResourceTable.Id_btn_change_mode);
        btnChangeMode.setClickedListener(new Component.ClickedListener() {
            private int index = 0;

            @Override
            public void onClick(Component component) {
                ListDialog listDialog = new ListDialog(MainAbilitySlice.this);
                listDialog.setSingleSelectItems(new String[]{"LEFT_OVERLAY", "RIGHT_OVERLAY", "NONE"}, parallaxViewPager.getSlideMode().getValue());
                listDialog.setOnSingleSelectListener(new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int position) {
                        LogUtil.info("MainAbilitySlice", " " + position);
                        index = position;
                        switch (position) {
                            case LEFT_OVERLAY:
                                parallaxViewPager.setSlideMode(Mode.LEFT_OVERLAY);
                                break;
                            case RIGHT_OVERLAY:
                                parallaxViewPager.setSlideMode(Mode.RIGHT_OVERLAY);
                                break;
                            case NONE:
                                parallaxViewPager.setSlideMode(Mode.NONE);
                                break;
                        }
                        listDialog.hide();
                    }
                });
                listDialog.show();

            }
        });
    }

    private void initView() {
        parallaxViewPager = (ParallaxViewPager) findComponentById(ResourceTable.Id_pvp_main);
        pageview = new ArrayList<Component>();
        ImageDirectionalLayout imageDirectionalLayout1 = new ImageDirectionalLayout(this, ResourceTable.Media_1);
        imageDirectionalLayout1.setId(1);
        pageview.add(imageDirectionalLayout1);
        ImageDirectionalLayout imageDirectionalLayout2 = new ImageDirectionalLayout(this, ResourceTable.Media_2);
        imageDirectionalLayout2.setId(2);
        pageview.add(imageDirectionalLayout2);
        ImageDirectionalLayout imageDirectionalLayout3 = new ImageDirectionalLayout(this, ResourceTable.Media_3);
        imageDirectionalLayout3.setId(3);
        pageview.add(imageDirectionalLayout3);
        ImageDirectionalLayout imageDirectionalLayout4 = new ImageDirectionalLayout(this, ResourceTable.Media_4);
        imageDirectionalLayout4.setId(4);
        pageview.add(imageDirectionalLayout4);
        ImageDirectionalLayout imageDirectionalLayout5 = new ImageDirectionalLayout(this, ResourceTable.Media_5);
        imageDirectionalLayout5.setId(5);
        pageview.add(imageDirectionalLayout5);
        ImageDirectionalLayout imageDirectionalLayout6 = new ImageDirectionalLayout(this, ResourceTable.Media_6);
        imageDirectionalLayout6.setId(6);
        pageview.add(imageDirectionalLayout6);
        ImageDirectionalLayout imageDirectionalLayout7 = new ImageDirectionalLayout(this, ResourceTable.Media_7);
        imageDirectionalLayout7.setId(7);
        pageview.add(imageDirectionalLayout7);
        ImageDirectionalLayout imageDirectionalLayout8 = new ImageDirectionalLayout(this, ResourceTable.Media_8);
        imageDirectionalLayout8.setId(8);
        pageview.add(imageDirectionalLayout8);
        parallaxViewPager.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return pageview.size();
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int position) {
                componentContainer.addComponent(pageview.get(position));
                return pageview.get(position);
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
                componentContainer.removeComponent(pageview.get(position));
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object object) {
                return component == object;
            }
        });
        parallaxViewPager.setSlideMode(Mode.LEFT_OVERLAY);
        parallaxViewPager.setInterpolator(new DecelerateInterpolator());
    }
}
